import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class AlphaChannelDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.current_color = QColor(255, 0, 0, 255)  # 红色，不透明
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        # 创建两个显示区域来演示透明度效果
        container = QWidget()
        container_layout = QHBoxLayout(container)
        
        self.solid_display = QLabel('不透明背景')
        self.solid_display.setMinimumSize(150, 100)
        self.solid_display.setStyleSheet("background-color: white; border: 1px solid black;")
        
        self.alpha_display = QLabel('透明效果')
        self.alpha_display.setMinimumSize(150, 100)
        self.alpha_display.setStyleSheet("""
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                stop:0 white, stop:0.45 white, stop:0.55 gray, stop:1 gray);
            border: 1px solid black;
        """)
        
        # 在透明效果显示区域上叠加颜色
        self.alpha_color_label = QLabel('透明颜色')
        self.alpha_color_label.setAlignment(Qt.AlignCenter)
        self.alpha_color_label.setStyleSheet(f"background-color: {self.current_color.name()};")
        
        # 使用覆盖布局来显示透明效果
        overlay_layout = QVBoxLayout(self.alpha_display)
        overlay_layout.addWidget(self.alpha_color_label)
        
        container_layout.addWidget(self.solid_display)
        container_layout.addWidget(self.alpha_display)
        
        self.info_label = QLabel()
        
        alpha_btn = QPushButton('选择带透明度的颜色')
        alpha_btn.clicked.connect(self.select_alpha_color)
        
        layout.addWidget(QLabel("左：不透明背景 | 右：透明效果（棋盘格背景）"))
        layout.addWidget(container)
        layout.addWidget(self.info_label)
        layout.addWidget(alpha_btn)
        
        self.update_display()
        self.setLayout(layout)
        self.setWindowTitle('透明度支持演示')
        
    def select_alpha_color(self):
        from PyQt5.QtWidgets import QColorDialog
        
        color = QColorDialog.getColor(
            initial=self.current_color,
            parent=self,
            title="选择颜色（支持透明度）",
            options=QColorDialog.ShowAlphaChannel  # 启用透明度支持
        )
        
        if color.isValid():
            self.current_color = color
            self.update_display()
            
    def update_display(self):
        """更新颜色显示"""
        # 更新不透明显示
        self.solid_display.setStyleSheet(f"background-color: {self.current_color.name()}; border: 1px solid black;")
        
        # 更新透明显示
        self.alpha_color_label.setStyleSheet(f"background-color: {self.current_color.name()};")
        
        # 显示详细信息
        self.info_label.setText(
            f"RGBA: ({self.current_color.red()}, {self.current_color.green()}, "
            f"{self.current_color.blue()}, {self.current_color.alpha()})\n"
            f"HEX: {self.current_color.name()} (Alpha: {self.current_color.alpha()})"
        )


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = AlphaChannelDemo()
    window.show()
    sys.exit(app.exec_())
